import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
// 如果想要在router中使用store，必须引入
import store from "@/store"
import { Toast } from 'vant'

Vue.use(VueRouter)

const routes = [
  {
    path: "/",
    redirect: "/home"
  },
  {
    path: '/home',
    name: 'Home',
    meta:{num:0},
    component: Home,
    children: [
      {
        path: "popup",
        name: "popup",
        component: () => import("../components/popup/index.vue")
      },
    ]
  },
  {
    path: "/topic",
    meta: { num: 1 },
    component: () => import("../views/Topic.vue")
  },
  {
    path: "/category",
    meta: { num: 2 },
    children: [
      {
        path: "popup",
        name: "popup",
        component: () => import("../components/popup/index.vue")
      },
    ],
    component: () => import("../views/Category.vue")
  },
  {
    path: "/cart",
    meta: { num: 3 },
    component: () => import("../views/Cart.vue")
  },
  {
    path: "/user",
    meta: { num: 4 },
    component: () => import("../views/User.vue")
  },
  {
    path:"/channel/:id",
    component: () => import("../views/ChannelPag.vue")
  },
  {
    path: "/productdetail/:id",
    component: () => import("../views/ProductDetail.vue")
  },
  {
    path: "/brandetail/:id",
    component: () => import("../views/BranDetail.vue")
  },
  {
    path: "*",
    component: () => import("../components/404.vue")
  },
  
]

const router = new VueRouter({
  // mode: 'history',
  base: process.env.BASE_URL,
  routes
})

let time;
// 导航
router.beforeEach((to, from, next) => {
  clearTimeout(time)
  if (to.path == "/home" || to.path == "/category"){
    store.commit('changeShowPopup', false);
  }
  if (to.path =="/cart"){
    if (!localStorage.getItem("token")){
      Toast("请先登录");
      time=setTimeout(() => {
        next("/user")
        return;
      }, 2000);
    }
  }
  next();
})

router.afterEach((to, from) => {
  if (from.name == "popup") {
    store.commit('changeShowList', false)
  }
})
export default router
